#include "PageLib.h"
#include "DirScanner.h"
#include "FileProcessor.h"
#include <iostream>
#include <fstream>

PageLib::PageLib(Configuration *config)
: _config(config){
    string path = _config->parseConfig("PAGE_PATH");
    _dirScanner(path);
}

PageLib::~PageLib(){

}

//创建网页库和位置偏移库
void PageLib::create(){
    FileProcessor fileProcessor(_dirScanner.files());
    fileProcessor.parseRss();
    _files = fileProcessor.dump();
    size_t posNow = 0;
    int n = 1;
    for(auto &i: _files){
        /* cout << i << endl; */
        int docid = n++;
        int pos = posNow;
        int len = i.size();
        posNow += len;
        _offsetLib[docid] = {pos, len};
    }
    cout << "网页库中的网页数: " << _files.size() << "\n";
    cout << "创建网页库和位置偏移库成功\n";
}

//存储网页库和位置偏移库
void PageLib::store(){
    ofstream ofs1(_config->parseConfig("WEBPAGE"));
    ofstream ofs2(_config->parseConfig("OFFSET"));
    for(auto &i: _files){
        ofs1 << i;
    }
    for(auto &i: _offsetLib){
        ofs2 << i.first << " " << i.second.first << " " << i.second.second << "\n";
    }
    cout << "存储网页库和位置偏移库成功\n";
}
